package org.jz.gae.redbook.spb.client.slate.species;

import org.jz.gae.redbook.spb.client.slate.family.FamilyChoosePlate;
import org.jz.gae.redbook.spb.client.toolkit.Slate;
import org.jz.gae.redbook.spb.shared.FamilyEntityView;
import org.jz.gae.redbook.spb.shared.SpeciesEntityView;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SpeciesPropertiesPanel extends Composite 
{
	  private final TextBox speciesFamilyPresentation = new TextBox();
	  private final TextBox speciesLatName = new TextBox();
	  private final TextBox speciesRusName = new TextBox();
	  private final TextArea speciesNote = new TextArea();
	  private final FlexTable detailsTable;
	  private final Button chooseFamilyButton = new Button("...");
	  private String speciesFamilyKey;
	  private Slate slate; 
	  
	  public SpeciesPropertiesPanel()
	  {
		  SimplePanel contentDetailsDecorator = new SimplePanel();
	    contentDetailsDecorator.setWidth("18em");
	    initWidget(contentDetailsDecorator);

	    VerticalPanel contentDetailsPanel = new VerticalPanel();
	    contentDetailsPanel.setWidth("100%");

	    // Create the contacts list
	    //
	    detailsTable = new FlexTable();
	    detailsTable.setCellSpacing(0);
	    detailsTable.setWidth("100%");
	    detailsTable.addStyleName("contacts-ListContainer");
	    detailsTable.getColumnFormatter().addStyleName(1, "add-contact-input");
	    initDetailsTable();
	    contentDetailsPanel.add(detailsTable);
	    
	    contentDetailsDecorator.add(contentDetailsPanel);
	  }
	  
	  private void initDetailsTable() 
	  {
		speciesFamilyPresentation.setEnabled(false);  
		  
	    detailsTable.setWidget(0, 0, new Label("Семейство:"));
	    detailsTable.setWidget(0, 1, speciesFamilyPresentation);
	    detailsTable.setWidget(0, 2, chooseFamilyButton);
	    detailsTable.setWidget(1, 0, new Label("Латинское наименование:"));
	    detailsTable.setWidget(1, 1, speciesLatName);
	    detailsTable.setWidget(3, 0, new Label("Русское наименование:"));
	    detailsTable.setWidget(3, 1, speciesRusName);
	    detailsTable.setWidget(4, 0, new Label("Примечание:"));
	    detailsTable.setWidget(4, 1, speciesNote);
	    speciesLatName.setFocus(true);
	    chooseFamilyButton.addClickHandler(new ClickHandler(){
	    	@Override
	    	public void onClick(ClickEvent event)
	    	{
	    		chooseFamily();
	    	}
	    });
	  }

	public SpeciesEntityView pickUpInformation()
	{
		SpeciesEntityView species = new SpeciesEntityView();
		/**/
	    species.setSpeciesFamilyKey(speciesFamilyKey);
	    species.setSpeciesFamilyPresentation(speciesFamilyPresentation.getValue());
	    species.setSpeciesLatName(speciesLatName.getValue());
	    species.setSpeciesRusName(speciesRusName.getValue());
	    species.setSpeciesNote(speciesNote.getValue());
		return species;
	}
	
	public void displayInformation(
			SpeciesEntityView _Species)
	{
	    speciesFamilyKey = _Species.getSpeciesFamilyKey();
	    speciesFamilyPresentation.setValue(_Species.getSpeciesFamilyPresentation());
	    speciesLatName.setValue(_Species.getSpeciesLatName());
	    speciesRusName.setValue(_Species.getSpeciesRusName());
	    speciesNote.setValue(_Species.getSpeciesNote());
	}
	
	public void lockInterface()
	{
	    speciesFamilyPresentation.setEnabled(false);
	    speciesLatName.setEnabled(false);
	    speciesRusName.setEnabled(false);
	    speciesNote.setEnabled(false);
	    chooseFamilyButton.setEnabled(false);
	    chooseFamilyButton.setVisible(false);
	}
	
	public void setSlate(Slate slate)
	{
		this.slate = slate;
	}
	
	public Slate getSlate()
	{
		return slate;
	}	
	
	private void chooseFamily()
	{
		try
		{
			FamilyChoosePlate plate = new FamilyChoosePlate();
			plate.chooseFamily(slate, new FamilyChoosePlate.FamilyChoosingListener()
			{
				
				@Override
				public void familyChoosen(
						FamilyEntityView _Family)
				{
					System.err.println("family chosen : " + _Family.getFamilyKey());
					speciesFamilyKey = _Family.getFamilyKey();
					speciesFamilyPresentation.setValue(_Family.getFamilyLatName());
				}
			});
		}
		catch(Exception ex)
		{
			Window.alert("Error : " + ex.getMessage());
		}
	}
}
